import FireBaseMgr from "./FireBaseMgr";

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
    },

    // LIFE-CYCLE CALLBACKS:

    onLoad () {

    },

    start () {

    },

    // update (dt) {},

    getPokerFramePath:function (pokerId) {
        let pokerFlower = cc.gg.dataMgr.getPokerHua(pokerId)
        let pokerPathNum = 0
        if(pokerFlower === cc.gg.enumConfig.FLOWER.BLACK){
            pokerPathNum = 100
        }else if(pokerFlower === cc.gg.enumConfig.FLOWER.HEART){
            pokerPathNum = 200
        }else if(pokerFlower === cc.gg.enumConfig.FLOWER.FLOWER){
            pokerPathNum = 300
        }else if(pokerFlower === cc.gg.enumConfig.FLOWER.DIAMOND){
            pokerPathNum = 400
        }else if(pokerFlower === cc.gg.enumConfig.FLOWER.JOKER){
            pokerPathNum = 500
        }

        let pokerNum = cc.gg.dataMgr.getPokerNum(pokerId)
        pokerPathNum += pokerNum

        return pokerPathNum
    },


    setPokerFrame:function (sprite, path) {
        cc.loader.loadRes(path,cc.SpriteFrame,function (err, frame) {
            if(err){
                cc.error(err)
                return
            }

            if(!frame){
                cc.error("no frame for poker")
                return
            }

            if(!cc.isValid(sprite.node)){
                console.warn("要改变frame的扑克节点已经不存在！")
                return
            }

            if(!sprite){
                cc.error("no sprite of poker")
                return
            }

            if(!cc.isValid(sprite.node,true)){
                cc.error("The poker node is already destoryed")
                return
            }

            sprite.spriteFrame = frame
        }.bind(this))
    },

    setPokerFrameSimple:function (sprite, pokerId) {
        let path = this.getPokerFramePath(pokerId)
        this.setPokerFrame(sprite,"pokers/"+path)
    },

    async loadRes(path){
        return new Promise((resole)=>{
            cc.loader.loadRes(path,cc.Prefab,function (err, prefab) {
                if(err){
                    cc.error(err)
                    resole(null)
                }else resole(prefab)
            })
        })
    }
});
